# Contributor: Sören Tempel <soeren+alpine@soeren-tempel.net>
# Contributor: Johannes Matheis <jomat+alpinebuild@jmt.gr>
# Maintainer: Johannes Matheis <jomat+alpinebuild@jmt.gr>
pkgname=mumble
pkgver=1.4.230
_libver=1.4.0
pkgrel=6
pkgdesc="Low-latency, high quality voice chat software"
url="https://wiki.mumble.info/"
arch="all"
license="BSD-3-Clause"
options="!check"
depends="qt5-qtbase-sqlite"
makedepends="
	alsa-lib-dev
	avahi-dev
	boost-dev
	cmake
	libcap-dev
	libsndfile-dev
	libxi-dev
	opus-dev
	poco-dev
	protobuf-dev
	qt5-qtbase-dev
	qt5-qtsvg-dev
	qt5-qttools-dev
	samurai
	speex-dev
	speexdsp-dev
	"
install="murmur.pre-install"
pkgusers="murmur"
pkggroups="murmur"
subpackages="
	$pkgname-doc
	murmur
	murmur-openrc:murmur_openrc:noarch
	murmur-doc:murmur_doc:noarch
	"
source="https://github.com/mumble-voip/mumble/releases/download/v$pkgver/mumble-$pkgver.tar.gz
	murmur.initd

	poll.patch
	libgen.patch
	no-rnnoise.patch
	musl-1.2.3.patch
	Find-and-link-Poco-XML.patch
	"
builddir="$srcdir/$pkgname-$pkgver.src"

# secfixes:
#   1.3.4-r0:
#     - CVE-2021-27229
#   1.2.19-r9:
#     - CVE-2018-20743

build() {
	cmake -B build -G Ninja \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_INSTALL_LIBDIR=lib \
		-DBUILD_SHARED_LIBS=True \
		-DCMAKE_BUILD_TYPE=MinSizeRel \
		-DMUMBLE_INSTALL_PLUGINDIR=/usr/lib/mumble \
		-Doverlay-xcompile=OFF \
		-Dspeechd=OFF \
		-Dice=OFF \
		-Drnnoise=OFF \
		-Dbundled-celt=ON \
		-Dbundle-qt-translations=OFF \
		-Dbundled-speex=OFF \
		-Dbundled-opus=OFF \
		.
	cmake --build build
}

package() {
	# Binaries.
	local _maj=$( echo "$_libver" | cut -d. -f1)
	local _min=$( echo "$_libver" | cut -d. -f2)
	install -Dm755 ./build/mumble \
		"$pkgdir"/usr/bin/mumble
	install -Dm755 ./scripts/mumble-overlay \
		"$pkgdir"/usr/bin/mumble-overlay

	# Libraries.
	install -Dm755 ./build/libmumbleoverlay.so.$_libver \
		"$pkgdir"/usr/lib/mumble/libmumbleoverlay.so.$_libver
	# XXX: Do we still need these symlinks?
	for lib in libmumbleoverlay.so libmumbleoverlay.so.$_maj libmumbleoverlay.so.$_maj.$_min; do
		ln -s libmumbleoverlay.so.$_libver \
			"$pkgdir"/usr/lib/mumble/$lib
	done
	install -Dm755 ./build/plugins/liblink.so \
		"$pkgdir"/usr/lib/mumble/liblink.so
	install -Dm755 ./build/libcelt* \
		"$pkgdir"/usr/lib/mumble/

	# Misc.
	install -Dm644 ./LICENSE \
		"$pkgdir"/usr/share/licenses/$pkgname/LICENSE
	install -Dm644 ./scripts/org.mumble_voip.mumble.desktop \
		"$pkgdir"/usr/share/applications/org.mumble_voip.mumble.desktop
	install -Dm644 ./icons/mumble.svg \
		"$pkgdir"/usr/share/icons/hicolor/scalable/apps/mumble.svg

	# Man pages.
	mkdir -p "$pkgdir"/usr/share/man/man1/
	install -m644 ./man/mum* "$pkgdir"/usr/share/man/man1/

	# openrc service
	install -Dm755 "$srcdir"/murmur.initd "$pkgdir"/etc/init.d/murmur
}

murmur() {
	pkgdesc="Murmur is the server component of Mumble"
	cd "$builddir"

	# Regarding the murmurd <-> mumble-server naming
	# See: https://github.com/mumble-voip/mumble
	# Currently, we continue using murmurd everywhere
	# but provide a murmurd -> mumble-server symlink.
	install -o murmur -g murmur -dm755 \
		"$subpkgdir"/var/lib/murmur
	install -Dm755 ./build/mumble-server \
		"$subpkgdir"/usr/bin/murmurd
	ln -s /usr/bin/murmurd "$subpkgdir"/usr/bin/mumble-server
	install -Dm644 ./scripts/murmur.ini \
		"$subpkgdir"/etc/murmur.ini

	sed 's/^database=$/database=\/var\/lib\/murmur\/murmur.sqlite/;\
		s/^;uname=$/uname=murmur/;\
		s/^;logfile=murmur.log$/logfile=\/var\/log\/murmur.log/;\
		s/^;pidfile=$/pidfile=\/var\/run\/murmur\/murmur.pid/'\
		-i "$subpkgdir"/etc/murmur.ini

	install -Dm644 ./scripts/murmur.conf \
		"$subpkgdir"/usr/share/dbus-1/system.d/murmurd.conf
	sed "s/mumble-server/murmur/" \
		-i "$subpkgdir"/usr/share/dbus-1/system.d/murmurd.conf
}

murmur_doc() {
	pkgdesc="Murmur is the server component of Mumble"

	cd "$builddir"
	install -Dm644 ./man/murmurd.1 \
		"$subpkgdir"/usr/share/man/man1/murmurd.1
	install -Dm644 ./LICENSE \
		"$subpkgdir"/usr/share/licenses/murmur/LICENSE
	default_doc
}

murmur_openrc() {
	default_openrc
	install_if="murmur=$pkgver-r$pkgrel openrc"
}

sha512sums="
6cffc7a95d88b33876f4093b99266468210f5c14f190fbd2fbe4991bef91a567e55296e7c8c6cc99e19c054853211085cc3cc08109e367e6776afb70766b3a53  mumble-1.4.230.tar.gz
1edccadd87446a6b9f5b87f19405f92e3450c48334c79fba4bc2c31224be767be084488c4352f14e64b7506b5963d1d6f3d8d4c7ea8e8276104d19a4a03892c6  murmur.initd
2d35b71ce9b537fc3c1d9ec1a280a20209aa0e751b4a4bc8b3ea0d97418787ec19c708d88b51b3acab69f51a601dfa3ae49956afd78bbbe1122f879a58284f5e  poll.patch
2dac9e8e321c7a9837a0e4e2b368c39ffa525012316063e02ae5286c215967e88007a4d89590c251e11020e3d50f8f08eb050a3a02526f758d128b7b061580d2  libgen.patch
7b82a4333e49253e0a6048741a710bace18ebbf156565365b843888d7f3e02cc9e43c411e2e9a914109f14db2b45f1db0a393cbd8399ec9ccf27d60419e551ff  no-rnnoise.patch
2cc0a17a31bf9a436ea97447f958b7a93e96b0fcd020e5389ccfc413b6498b5176294d2b9c788a68cdfac47dabca8e6e28699f5ad184dcde4d30feb88d2e99b3  musl-1.2.3.patch
04de486f19f2893f55f1ab14531b2a13f12e40f07ba3feec92fe0d86190c6da91cb96691f17ead15927764409e73ccba7aa54328e9e6e5dd9bcc3784b727c562  Find-and-link-Poco-XML.patch
"
